package com.healer.config;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;


import static com.healer.common.CookieUtils.getCookieMap;
@Component
public class MyInterceptor implements HandlerInterceptor {
    static Logger logger= Logger.getLogger(MyInterceptor.class);
   @Autowired
   private JedisPool jedisPool;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        if(request.getRequestURI().contains("queryuser")||request.getRequestURI().contains("403")) {
//            logger.info("这不是爸爸吗，来请过");
//            return true;
//        }
        Jedis jedis=jedisPool.getResource();
        if(jedis==null) {
            request.getRequestDispatcher("error/500").forward(request,response);
            return false;
        }
        if(request.getRequestURI().contains("queryuser"))
            logger.warn("你拦我login做什么？");

        String[] check={"name","password","systemInfo"};

        Cookie[] cookies = request.getCookies();
        Map<String,String> cookieMap = getCookieMap(cookies);
        logger.info("本次访问的cookies:"+request.getRequestURI()+","+cookieMap.toString());
        String JSESSIONID = cookieMap.get("JSESSIONID");
        if(cookieMap==null) {
            logger.error("未登录");
            request.getRequestDispatcher("error/403").forward(request,response);
            jedis.close();
            return false;
        }
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        logger.info("本次访问的sessionId:"+request.getRequestURI()+","+sessionId);

        if(JSESSIONID.equals(sessionId)){
            for(String i:check){
                logger.info("session:"+i+"-->"+jedis.hget(sessionId,i)+":"+cookieMap.get(i));
//                logger.debug("测试："+!jedis.hexists(sessionId,i)+":"+(cookieMap.get(i)==null)+":"+(jedis.hget(sessionId,i).equals(cookieMap.get(i))));
                if(!jedis.hexists(sessionId,i) ||
                        cookieMap.get(i)==null ||
                        !jedis.hget(sessionId,i).equals(cookieMap.get(i))) {
                    jedis.close();
                    logger.error("cookie和session不匹配");
                    request.getRequestDispatcher("error/403").forward(request,response);
                    return false;
                }
            }
        }
        else{
            jedis.close();
            request.getRequestDispatcher("error/403").forward(request,response);
            logger.error("cookie没有sessionId");
            return false;
        }
        jedis.close();
        return true;
    }
}
